#include <stdio.h>
#define ROWS 3
#define COLS 4

void sum_rows(int arr[][COLS], int rows);
void sum_cols(int [][COLS], int);
int sum2d(int (*arr)[COLS], int rows);

int main(void) {
    int junk[ROWS][COLS] = {
            {2, 4, 6, 8},
            {3, 5, 7, 9},
            {12, 10, 8, 6}
    };
   
    sum_rows(junk, ROWS);
    sum_cols(junk, ROWS);
    printf("Sum if all elements = %d\n", sum2d(junk, ROWS)); 

    return 0;
}

void sum_rows(int arr[][COLS], int rows) {
    int i, j, t;
    for (i = 0; i < rows; i++) {
        for (j = 0, t = 0; j < COLS; j++)
            t += arr[i][j];
        printf("row %d: sum = %d\n", i, t);
    }
}

void sum_cols(int arr[][COLS], int rows) {
    int i, j, t;
    for (i = 0; i < COLS; i++) {
        for (j = 0, t = 0; j < rows; j++)
            t += arr[j][i];
        printf("col %d: sum = %d\n", i, t);
    }
}

int sum2d(int arr[][COLS], int rows) {
    int i, j, t;
    t = 0;
    for (i = 0; i < rows; i++)
        for (j = 0; j < COLS; j++)
            t += arr[i][j];

    return t;
}
